<?php

namespace app\admin\model;

use think\Model;

/**
 * Orders
 */
class Orders extends Model
{
    // 表名
    protected $name = 'orders';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;
    protected $updateTime = false;

    // 字段类型转换
    protected $type = [
        'pay_time' => 'timestamp:Y-m-d H:i:s',
        'create_time' => 'timestamp:Y-m-d H:i:s',
    ];
    protected $append = [
        'textarea_text',
        'cx_status',
        'check_status_text',
        'status_text',
        'domain_overimg'
        // 'create_time_text'
    ];
    public function getDomainOverimgAttr($value, $data)
    {
        if(isset($data['overimg'])&&!empty($data['overimg'])){
            return request()->domain().$data['overimg'];
        }
        return  '';
    }

    public function getCxStatusAttr($value, $data)
    {
        //
        $arr = [
            0 => '进行中', 1=>'已完成', 2=>'已取消'
        ];
        if (isset($data['cxstatus'])) {
            return $arr[$data['cxstatus']];
        }
        return '-';
    }
    public function getStatusTextAttr($value, $data)
    {
        //
        $arr = [
            0 => '待充值', 1=>'充值中', 2=>'已完成',3=>'已取消'
        ];
        if (isset($data['status'])) {
            return $arr[$data['status']];
        }
        return '-';
    }
    public function getCheckStatusTextAttr($value, $data)
    {
        //0=>'进行中','已完成','已取消'
        $arr = [
            0 => '已下单', 1 => '已完成'
        ];
        //
        if (isset($data['check_status'])) {
            return $arr[$data['check_status']];
        }
        return '-';
    }

//    public function getCreateTimeAttr($value,$data)
//    {
//       // return date(''$value['create_time'];
//    }
    public function getTextareaTextAttr($value, $data)
    {
        if (isset($data['textarea']) && !empty($data['textarea'])) {
            return json_decode($data['textarea'], true);
        }
        return [];
    }

    public function appfuns(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\Appfuns::class, 'appfuns_id', 'id');
    }

    public function business(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\Business::class, 'appfuns_id', 'id');
    }

    public function getUpimgsAttr($value, $data)
    {
        if (empty($value)) {
            return '';
        }
        return request()->domain() . $value;
    }

    public function user(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\common\model\User::class, 'user_id', 'id');
    }

    public function masters(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\common\model\User::class, 'master_id', 'id');
    }

    public function orderNum($perfix = '')
    {
        $yearCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T');
        $year = date('Y');
        $number = $year . strtoupper(dechex(date('m'))) . date('d')
            . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
        return $number . $perfix;
    }
}